package wechat.response.business.event.message;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import wechat.bean.response.MessageBaseBean;
import wechat.bean.response.message.reply.ReplyImageTextBean;
import wechat.bean.response.message.reply.ReplyImageTextItemBean;
import wechat.resources.AppFilter;
import wechat.resources.InstanceResource;

public class ReceiveNewsBusiness {
	private Logger log = LoggerFactory.getLogger(ReceiveNewsBusiness.class);
	
	private ReplyImageTextBean replyImageTextBean;
	private InstanceResource instanceResource;
	
	public ReceiveNewsBusiness(ReplyImageTextBean replyImageTextBean){
		this.replyImageTextBean = replyImageTextBean;
	}
	
	public ReceiveNewsBusiness(String appid,ReplyImageTextBean replyImageTextBean){
		if (appid != null) {
			instanceResource = AppFilter.chooseApp(appid);
		}
		this.replyImageTextBean = replyImageTextBean;
	}
	
	public ReceiveNewsBusiness(String str) throws Exception{
		this.replyImageTextBean = new ReplyImageTextBean(str);
		this.replyImageTextBean.transform();
	}
	
	public ReceiveNewsBusiness(String appid,String str) throws Exception{
		if (appid != null) {
			instanceResource = AppFilter.chooseApp(appid);
		}
		this.replyImageTextBean = new ReplyImageTextBean(str);
		this.replyImageTextBean.transform();
	}
	
	public ReceiveNewsBusiness(InstanceResource instanceResource,String str) throws Exception{
		this.instanceResource = instanceResource;
		this.replyImageTextBean = new ReplyImageTextBean(str);
		this.replyImageTextBean.transform();
	}
	
	
	public String getReplyNews(List<ReplyImageTextItemBean> articles) throws Exception{
		String openId = replyImageTextBean.getFromUserName();
		
		ReplyImageTextBean replyImageTextBean = new ReplyImageTextBean();
		
		replyImageTextBean.setFromUserName(instanceResource.getCorpid());	
		replyImageTextBean.setToUserName(openId);
		replyImageTextBean.setCreateTime(System.currentTimeMillis());
		replyImageTextBean.setMsgType(MessageBaseBean.Reply_Message_News);
		replyImageTextBean.setArticles(articles);
		
		return replyImageTextBean.buildXml();
	}

	public InstanceResource getInstanceResource() {
		return instanceResource;
	}
}
